// order.js
var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var app = getApp();
var is_no_more = false;
var is_loading = false;
var p = 2;
var limit=5;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        status: -1,
        orderList: [],
        show_no_data_tip: false,//没有订单的标识
        hide:1,
        qrcode:"",
        imgUrl:api.RootUrl,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var page = this;
        is_no_more = false;
        is_loading = false;
        p = 2;
        console.log(options.status||-1);
        page.loadOrderList(options.status || -1);
        var pages = getCurrentPages();
        if (pages.length < 2) {
            page.setData({
                show_index: true,
            });
        }
    },

    loadOrderList: function (status) {
        if (status == undefined)
            status = -1;
        var page = this;
        page.setData({
            status: status,
        });
        wx.showLoading({
            title: "正在加载",
            mask: true
        });
        util.request(api.OrderList, { status: page.data.status, page: 1, limit: limit},'post').then(function(res){
            if(res. errno == 0){
              wx.hideLoading();
              page.setData({
                orderList: res.data.orderList,
              });
              }
              page.setData({
                show_no_data_tip: (page.data.orderList.length == 0),
              });
        })
    },


    onReachBottom: function () {
        var page = this;
        if (is_loading || is_no_more)
            return;
        is_loading = true;
        util.request(api.OrderList, { status: page.data.status, page: p ,limit:limit}, 'post').then(function (res) {
          if (res.errno == 0) {
            var orderList = page.data.orderList.concat(res.data.orderList);
            page.setData({
              orderList: orderList,
            });
            if (res.data.orderList.length == 0) {
              is_no_more = true;
            }
          }
          p++;
        });
    
    },

  urgeOrder:function(e){
    wx.showToast({
      title: '催单成功',
    })
  },

    /*
      订单支付
    */
    orderPay: function (e) {
      var orderId = e.currentTarget.dataset.id;
      var actualPrice = e.currentTarget.dataset.actualPrice;
      wx.redirectTo({
        url: "/pages/pay/pay?orderId=" + orderId +"&actualPrice="+actualPrice,
      })
      
    },

    orderRevoke: function (e) {
        var page = this;
        wx.showModal({
            title: "提示",
            content: "是否取消该订单？",
            cancelText: "否",
            confirmText: "是",
            success: function (res) {
                if (res.cancel)
                    return true;
                if (res.confirm) {
                    wx.showLoading({
                        title: "操作中",
                    });
                    util.request(api.OrderRevoke,{orderId: e.currentTarget.dataset.id}).then(function(res){
                      wx.hideLoading();
                      wx.showModal({
                        title: "提示",
                        content: res.errmsg,
                        showCancel: false,
                        success: function (res) {
                          if (res.confirm) {
                            page.loadOrderList(page.data.status);
                          }
                        }
                      });
                    }); 
                }
            }
        });
    },

    orderConfirm: function (e) {
        var page = this;
        wx.showModal({
            title: "提示",
            content: "是否确认已收到货？",
            cancelText: "否",
            confirmText: "是",
            success: function (res) {
                if (res.cancel)
                    return true;
                if (res.confirm) {
                    wx.showLoading({
                        title: "操作中",
                    });
                    util.request(api.OrderCofirm, {orderId: e.currentTarget.dataset.id}).then(function(res){
                      wx.hideLoading();
                      wx.showToast({
                        title: res.errmsg,
                      });
                      if (res.errno == 0) {
                        page.loadOrderList(4);
                      }
                    }) 
                }
            }
        });
    },
    

});